import 'package:dio/dio.dart';
import 'model/Result.dart';

class HttpCore {
  factory HttpCore() => _getInstance();

  static HttpCore get instance => _getInstance();
  static HttpCore _instance;

  HttpCore._internal();

  static HttpCore _getInstance() {
    if (_instance == null) {
      _instance = new HttpCore._internal();
    }
    return _instance;
  }

  static const String GET = "get";
  static const String POST = "post";

  static const sss = "eyJhbGciOiJIUzUxMiJ9.eyJ1c2VySWQiOjEsImF1ZCI6InBpbmtlIiwiaXNzIjoicGlua2UiLCJpYXQiOjE1NDU4OTgwMTcsImV4cCI6MTU0NTk4NDQxN30.QmoRCZpDmphrlhuX6I-H1WedrLCFsDbOkHTjk48xcoiDFor4aMHjiorUjLQ6orMU2d4Dz1xQgYpucQqA2JqBCQ";
  Dio dio = new Dio(new Options(
    baseUrl: "https://pinkeweixinadmin.51maihaowu.com/",
    connectTimeout: 5000,
    receiveTimeout: 100000,
    headers: {
      "token": sss,
      "api": "1.0.0"
    },
  ));

  get(String url,dynamic data) async {
    Result result = await _request(url,method: GET,params: data);
//    Result result = new Result.fromJson(response.data);
    return result;
  }

  post(String url,dynamic data) async {
    Result result = await _request(url,method: POST,params: data);
    return result;
  }

  Future<Result> _request(String url,
      {String method, dynamic params}) async {
//    dio.onHttpClientCreate = (HttpClient client) {
//      client.findProxy = (uri) {
//        //proxy all request to localhost:8888
//        return "PROXY 172.23.235.153:8888";
//      };
//    };
    String errorMsg = "";
    int statusCode;
    try {
      Response<Map<String,dynamic>> response;
      if (method == GET) {
//        if (params != null && params.isNotEmpty) {
//          StringBuffer sb = new StringBuffer("?");
//          params.forEach((key, value) {
//            sb.write("$key" + "=" + "$value" + "&");
//          });
//          String paramStr = sb.toString();
//          paramStr = paramStr.substring(0, paramStr.length - 1);
//          url += paramStr;
//        }
        response = await dio.get(url);
      } else if (method == POST) {
        if (params != null ) {
          response = await dio.post(url, data: params);
        } else {
          response = await dio.post(url);
        }
      }

      statusCode = response.statusCode;
      Result result = new Result.fromJson(response.data);
      if (statusCode < 0) {
        errorMsg = "网络请求错误,状态码:" + statusCode.toString();
//        _handError(errorCallBack, errorMsg);
        return result;
      }
      return result;
    } catch (exception) {
      print(exception.toString());
//      _handError(errorCallBack, exception.toString());
    }
  }

}
//
//Dio initHttp() {
// String sss = "eyJhbGciOiJIUzUxMiJ9.eyJ1c2VySWQiOjEsImF1ZCI6InBpbmtlIiwiaXNzIjoicGlua2UiLCJpYXQiOjE1NDU3MDM1MTksImV4cCI6MTU0NTc4OTkxOX0.V6w-EE4cFuJAAOTrYicJnKrG-FhU25pKyyQ-4ptEHf0JgFmNKZqYf-ilbkcjhkc2ubQtRep-i7MmgLguW357Dg";
// final Dio dio = new Dio(new Options(
//   baseUrl: "https://pinkeweixinadmin.51maihaowu.com/",
//   connectTimeout: 5000,
//   receiveTimeout: 100000,
//   headers: {
//     "token": sss,
//     "api": "1.0.0"
//   },
// ));
// return dio;
//}


